<template>
  <div>
    <uc-table url="admin/inHiddenDanger/getInDangerPage" ref="followUserTable" name="消防隐患" table-border pageable :search-form="searchForm">
      <template slot="searchForm">
        <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline filter-container">
          <!--搜索输入框-->
          <el-form-item>
            <el-date-picker
                v-model="addTimeList"
                type="daterange"
                align="right"
                unlink-panels
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item v-if="enterPriseHidden">
            <el-select v-model="searchForm.enterpriseId" placeholder="请选择所属企业" style="width: 100%" clearable  @clear="clearInput">
              <el-option v-for="(item,index) in enterpriseDominantList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchForm.doneStatus" placeholder="请选择完成状态" clearable>
              <el-option :value=0 label="未完成"></el-option>
              <el-option :value=1 label="完成"></el-option>
            </el-select>
          </el-form-item>
          <!--搜索按钮-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch()">查询</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template slot="columns">
        <el-table-column prop="enterpriseName" label="所属企业" align="left" :show-overflow-tooltip="true"></el-table-column>
<!--        <el-table-column prop="taskId" label="任务Id" align="left" :show-overflow-tooltip="true"></el-table-column>-->
        <el-table-column prop="name" label="名称" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="address" label="地点" align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="abnormalPic" label="异常图片" align="left" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-image
                :src="scope.row.abnormalPic+'?imageView2/1/w/40/h/40'" class="user-avatar"
                :preview-src-list="[scope.row.abnormalPic]">
            </el-image>
          </template>
        </el-table-column>
          <el-table-column prop="status" label="隐患状态" align="left" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                  {{scope.row.status | hiddenDangerStatus}}
              </template>
          </el-table-column>
          <el-table-column prop="addTime" label="生成时间" align="left" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                  {{scope.row.addTime | dateFormat}}
              </template>
          </el-table-column>
<!--        <el-table-column prop="isEquipment" label="是否设备生成" align="left" :show-overflow-tooltip="true"></el-table-column>-->
        <el-table-column prop="remark" label="隐患描述" align="left" :show-overflow-tooltip="true"></el-table-column>

        <el-table-column prop="address" label="操作" width="180" align="left" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" icon="el-icon-info" type="success" @click="handleInfo(scope.row)">详情</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" :loading="deleteLoading" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </uc-table>
    </div>
</template>

<script>
import UcTable from "@/components/UcTable/index.vue";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {deleteInHiddenDanger} from "@/api/hiddenDanger";


export default {

  name: "HiddenDangerList",
  components: {UcTable},
  data(){
    return{
      searchForm:{
        deletel:false,
        doneStatus:null
      },
      addTimeList:[],
      enterpriseDominantList:[],
      deleteLoading:false,
      //快捷日期
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [{
          text: '15天内',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '15天-30天',
          onClick(picker) {
            const end = new Date();
            end.setTime(end.getTime() - 3600 * 1000 * 24 * 15);
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '30天-三个月',
          onClick(picker) {
            const end = new Date();
            end.setTime(end.getTime() - 3600 * 1000 * 24 * 30);
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    }
  },
  computed:{
    enterPriseHidden(){
      let roleCode = this.$store.getters.roleCode;
      return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"||roleCode=="XZ-ADMIN"
    }
  },
  created() {
    this.searchForm.doneStatus = this.$route.params.doneStatus
    if(this.searchForm.doneStatus==0){
      this.searchForm.customize = " and status!=5 "
    }else if(this.searchForm.doneStatus==1){
      this.searchForm.customize = " and status=5 "
    }
    if(this.enterPriseHidden()){
      this.getInEnterpriseDominantList()
    }
  },
  methods: {
    handleSearch(){
      // this.searchForm.startDate = null
      // this.searchForm.endDate = null
      if(this.addTimeList.length>0){
        this.searchForm.startDate = this.addTimeList[0]
        this.searchForm.endDate = this.addTimeList[1]
      }
      if(this.searchForm.doneStatus==0){
        this.searchForm.customize = " and status!=5 and status!=7 "
      }else if(this.searchForm.doneStatus==1){
        this.searchForm.customize = " and status in (5,7) "
      }
      this.$refs.followUserTable.getTableData(1)
    },
    getInEnterpriseDominantList(){
      getInEnterpriseDominantList({deleted:false}).then(res=>{
        this.enterpriseDominantList = res.data
      })
    },
    handleAdd(){
      this.$router.push("/nonEmergency/hiddenDangerEdit")
    },
    handleEdit(val) {
      this.$router.push("/nonEmergency/hiddenDangerEdit?ucId=" + val.ucId)
    },
    handleInfo(val) {
      this.$router.push("/nonEmergency/hiddenDangerInfo?ucId=" + val.ucId)
    },
    clearInput(){
      //清除输入框
       this.searchForm.enterpriseId = null
      this.handleSearch()
    },
    handleDelete(val) {
      this.$confirm('您确定要执行删除' + val.name +  '这条消防隐患吗？', '提醒信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteLoading = true
        deleteInHiddenDanger(val).then(res => {
          this.deleteLoading = false
          this.$notify.success({
            title: '成功',
            message: "删除消防隐患成功"
          })
          this.handleSearch(this.tabValue)
        }).catch(res => {
          this.deleteLoading = false
          this.$notify.error({
            title: '失败',
            message: res.msg
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

  }
}
</script>

<style scoped lang="scss">
.user-avatar {
  cursor: pointer;
  width: 20px;
  height: 20px;
}
</style>
